<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/11/23 0023
 * Time: 9:33
 */

namespace app\index\controller;

use app\index\model\School as schoolModel;
use app\index\model\Classes as classesModel;
use app\index\model\Course as courseModel;
use app\index\model\Student as studentModel;
use app\index\model\ScoreType as scoreTypeModel;
use think\facade\Cache;

class Score extends Base
{
    private $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\index\model\Score();
    }

    public function index()
    {
        $schoolModel = new schoolModel();
        $school = $schoolModel->getAll();
        $classesModel = new classesModel();
        $class = $classesModel->getAll();
        $courseModel = new courseModel();
        $course = $courseModel->getAll();
        $scoreTypeModel = new scoreTypeModel();
        $scoretype = $scoreTypeModel->getAll();
        $this->assign('school', $school);
        $this->assign('class', $class);
        $this->assign('course', $course);
        $this->assign('scoretype', $scoretype);
        return $this->fetch();
    }

    public function lst()
    {
        $start = $this->request->get('start', 0);
        $length  = $this->request->get('length', 15);
        $search  = $this->request->get('keyword');
        $school_id  = $this->request->get('school_id');
        $class_id  = $this->request->get('class_id');
        $score_id  = $this->request->get('score_id');
        $course_id  = $this->request->get('course_id');
        $where = [];
        if ($search) {
            $where[] = ['st.name', 'like', "%{$search}%"];
        }
        if ($school_id) {
            $where[] = ['s.school_id', '=', $school_id];
        }
        if ($class_id) {
            $where[] = ['s.class_id', '=', $class_id];
        }
        if ($score_id) {
            $where[] = ['s.score_id', '=', $score_id];
        }
        if ($course_id) {
            $where[] = ['s.course_id', '=', $course_id];
        }

        $data = $this->model->getList($start, $length, $where);
        $total = $this->model->getListCount($where);
        $result = [
            'draw' => $this->request->get('draw'),
            'recordsTotal' => $total,
            'recordsFiltered' => $total,
            'data' => $data
        ];
        return json($result);
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('data/a', []);
            $result = $this->validate($data, 'app\index\validate\Score');
            if ($result !== true) {
                $this->error($result);
            }
            $res = $this->model->add($data);
            if ($res !== false) {
                //设置默认选项
                $cache_data = array(
                    'school_id' => $data['school_id'],
                    'class_id' => $data['class_id'],
                    'stu_id' => $data['stu_id'],
                    'course_id' => $data['course_id'],
                    'score_id' => $data['score_id'],
                );
                Cache::set('score_cache', $cache_data);
                $this->success('添加成功');
            }
            $this->error('添加失败');
        }

        $schoolModel = new schoolModel();
        $school = $schoolModel->getAll();
        $classesModel = new classesModel();
        $class = $classesModel->getAll();
        $courseModel = new courseModel();
        $course = $courseModel->getAll();
        $studentModel = new studentModel();
        $student = $studentModel->getAll();
        $scoreTypeModel = new scoreTypeModel();
        $scoretype = $scoreTypeModel->getAll();
        $this->assign('school', $school);
        $this->assign('class', $class);
        $this->assign('course', $course);
        $this->assign('student', $student);
        $this->assign('scoretype', $scoretype);

        $score_cache = Cache::get('score_cache');
        $this->assign('school_id', $score_cache['school_id'] ? $score_cache['school_id'] : 0);
        $this->assign('class_id', $score_cache['class_id'] ? $score_cache['class_id'] : 0);
        $this->assign('stu_id', $score_cache['stu_id'] ? $score_cache['stu_id'] : 0);
        $this->assign('course_id', $score_cache['course_id'] ? $score_cache['course_id'] : 0);
        $this->assign('score_id', $score_cache['score_id'] ? $score_cache['score_id'] : 0);
        return $this->fetch();
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('data/a', []);
            $id = $this->request->post('id', 0);
            $result = $this->validate($data, 'app\index\validate\Score');
            if ($result !== true) {
                $this->error($result);
            }
            $res = $this->model->edit($id, $data);
            if ($res !== false) {
                $this->success('编辑成功');
            }
            $this->error('编辑失败');
        }
        $id = $this->request->get('id');
        $score = $this->model->getOneById($id);
        $this->assign('score', $score);

        $schoolModel = new schoolModel();
        $school = $schoolModel->getAll();
        $classesModel = new classesModel();
        $class = $classesModel->getAll();
        $courseModel = new courseModel();
        $course = $courseModel->getAll();
        $studentModel = new studentModel();
        $student = $studentModel->getAll();
        $scoreTypeModel = new scoreTypeModel();
        $scoretype = $scoreTypeModel->getAll();
        $this->assign('school', $school);
        $this->assign('class', $class);
        $this->assign('course', $course);
        $this->assign('student', $student);
        $this->assign('scoretype', $scoretype);

        return $this->fetch();
    }

    /**
     * 删除成绩
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $id = $this->request->get('id');
            $res = $this->model->del($id);
            if ($res !== false) {
                $this->success('删除成功');
            }
            $this->error('删除失败');
        }
    }
}